package org.example.linkedList;

/**
 * @Auther: wangbw
 * @Date:2020/11/12
 * @Description: org.example.linkedList
 * @version: 1.0
 */
public class LinkLoop {
    public static boolean isCycle(Node head){
        Node slow = head;
        Node fast = head;
        while (fast != null && fast.getNext() != null){
            fast = fast.getNext().getNext();
            slow = slow.getNext();
            if (fast == slow){
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Node head = Node.createNode(5);
        Node tail = head;
        while(tail!= null && tail.getNext() != null){
            tail = tail.getNext();
        }
        tail.setNext(head);
        boolean cycle = isCycle(head);
        System.out.println(cycle);
    }
}
